<template>
  <div class="chart" :id="id" width="400px"></div>
</template>

<script>
import mixins from '@/utils/mixins'
export default {
  name: "pie",
  mixins: [mixins],
  props: {
    id: {
      type: String,
      default: "",
    },
    title: {
      type: String,
      default: "",
    },
    seriesData: {
      type: Array,
      default: () => [],
    },
  },
  computed: {
    getOption() {
      let option = {
        color: ["#9499b3", "#00c693", "#ffbf3a", "#f68059"],
        title: {
          text: this.title,
          left: "left",
          textStyle: {
            fontSize: 18,
            color: "#eee",
          },
        },
        tooltip: {
          axisPointer: {
            type: "shadow",
          },
        },
        series: [
          {
            name: "地域分布",
            type: "pie",
            radius: ['50%', '70%'],
            center: ["50%", "60%"],
            data: this.seriesData,
            label: {
              show: true,
              position: "right",
              formatter: "{b}\n{c}人({d}%)",
            },
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
            minAngle: 1,
          },
        ],
      };
      return option;
    },
  },
  mounted() {
    const options = this.getOption;
    if (options) {
      this.myCharts = this.$echarts.init(document.getElementById(this.id));
      this.myCharts.setOption(options);
      this.handleResize(this.myCharts);
    }
  },
  beforeDestroy() {
    this.myCharts.clear();
  },
  watch: {
    getOption: {
      handler: function(nval) {
        this.myCharts.setOption(nval);
      },
      deep: true,
    },
  },
};
</script>

<style lang="scss" scoped>
.chart {
  width: 400px;
  height: 330px;
}
</style>
